Skip to content

Support super().prop for pyscript properties - #923

Merged
craigbarratt merged 1 commit into
custom-components:masterfrom
HoneyBearTech:support-super-property
Nov 22, 2026
Merged

craigbarratt merged 1 commit into
custom-components:masterfrom
HoneyBearTech:support-super-property

Conversation

@HoneyBearTech

Copy link
Copy Markdown
Contributor

What was broken
Since #918, obj.prop calls a pyscript property's getter, but super().prop still didn't. It returned a coroutine instead of the value, so the most common use, a subclass property that extends its parent's, failed:

class Base:
    @property
    def label(self):
        return "base"

class Child(Base):
    @property
    def label(self):
        return super().label + "+child"   # TypeError: a coroutine, not "base"

The same happened with super(Child, self).label, in a plain method as well as in a getter, and with a metaclass property read through super().

Why
Pyscript runs a pyscript getter itself, because Python can't call pyscript's async functions. To find the property it looks along type(obj).__mro__. For super().label the object is a super object, and type(obj) is super, which has no label. So pyscript left the lookup to Python, and Python called the pyscript getter like a normal function, which gives back a coroutine.

What I changed
As suggested in the review of #918, the property is now looked up in the MRO after __thisclass__ when the object is a super:

  • super_property(sup, attr) (new, in eval.py) finds the attribute the way Python's super does: it searches __self_class__.__mro__ after __thisclass__ and stops at the first class that has the attribute. It returns that attribute only when it is a plain property, and only when __self__ is not __self_class__. Like Python, it reads the super object's __self__, __self_class__ and __thisclass__, and each class's __mro__ and __dict__, through the built-in descriptors. A super object would otherwise look those names up in the parent classes first, and a metaclass's __getattribute__ or data descriptor could intercept a class's.
  • ast_attribute: when the object is exactly a super object and that property's getter is a pyscript function, checked by the getter's real type so that nothing on a native getter (such as a hooked __class__) is read, pyscript awaits the getter with the super object's own __self__. As in Python, an AttributeError from the getter goes straight to the caller, without the __getattr__ fallback that obj.prop has, and a class's own __getattribute__ isn't used. This check comes after the existing state-variable, assignment and __init__ handling. On other attribute loads it costs one type check.
  • Anything else is left to Python as before:
    • super().__class__, which Python never searches for, so it is still super even if a parent defines a __class__ property;
    • an unbound super(C);
    • super(C, cls).prop on a class, where Python returns the property object itself;
    • a nearer plain attribute or method that hides a farther property;
    • a property with no getter (same AttributeError message as Python);
    • a super object whose __thisclass__ is no longer in the MRO, for example after __bases__ changes (same AttributeError as Python);
    • a property subclass, a compiled (@pyscript_compile) or native getter, and a subclass of super.
  • Setting or deleting through super() isn't supported by Python, and it isn't here either: super().prop = value and del super().prop raise the same AttributeError as Python.

Testing
One new test in tests/test_unit_eval.py, test_super_property_match_python, with 28 scripts. Like the #918 tests, it runs each script in CPython and in pyscript and compares the results. It also checks that running each script in pyscript gives no "coroutine was never awaited" warning, except for the __class__ script, where isinstance() inside pyscript itself reads the pyscript __class__ getter. The scripts cover:

  • super().prop and super(C, self).prop in a method and in an overriding getter;
  • a chain of three classes, and a property in a mixin in a diamond-shaped hierarchy;
  • a getter that raises AttributeError, in a class with __getattr__: no fallback through super(), while obj.prop still falls back;
  • a compiled getter, and a native property (fractions.Fraction.numerator);
  • super().prop and super(C, cls).prop in a class method, which give the property object;
  • a class with its own __getattribute__, which super() doesn't use;
  • super(C, self).prop in a generator expression and in list comprehensions (also zero-argument super() in a comprehension), and zero-argument super() in a generator expression, which raises the same TypeError as Python;
  • a metaclass property read through super() and super(Meta, cls);
  • a __class__ property on a parent, where super().__class__ is still super;
  • a subclass of super with its own __getattribute__;
  • a nearer plain attribute or method hiding a farther property, a property with no getter, a property subclass, and an unbound super(C);
  • super() after the class's __bases__ change, and a super object whose __thisclass__ is no longer in the MRO;
  • setting and deleting through super(), and calling a parent's setter with super(C, type(self)).prop.fset(self, value) from an overriding setter;
  • a parent class with its own __self_class__ attribute, or compiled properties named __self__ or __thisclass__, which super() ignores, and a parent's __self__ attribute, which doesn't change the instance the getter gets;
  • a metaclass whose __getattribute__, or a data descriptor on it, blocks __mro__ or __dict__, which super() doesn't use;
  • a native getter whose __getattribute__ blocks __class__, which super() doesn't read.

Without this change, 10 of the 28 scripts fail. The other 18 check behaviour that already worked and must stay the same.

The full test suite, ruff, pylint, mypy, codespell and sphinx-build -W pass on Python 3.14. The extra check on ordinary attribute loads is one type check, about 17 ns, which is well under 1% of an attribute load in pyscript. On a microbenchmark that does little but attribute loads, the best of 50 runs was 1.51 s on master and 1.54 s with this change, which is inside the spread between rounds of the same code (up to 8%).

Docs
The @property item in Language Limitations no longer lists super().prop as not working. It now says:

  • super().prop and super(Cls, self).prop call a parent class's getter, so a subclass's property can extend its parent's;
  • as in Python, super().prop = value and del super().prop raise AttributeError, and super(Cls, type(self)).prop.fset(self, value) calls a parent class's setter;
  • a pyscript getter, setter or deleter on a property subclass still doesn't work, also through super();
  • reading a property through super() isn't affected by a class's own __getattribute__, since super() doesn't use it, as in Python.

Note
This is one commit on master after #918. My open #919 (lambdas) and #920 (StopIteration) also change eval.py and add tests at the end of tests/test_unit_eval.py. Whichever merges later needs a small rebase, and I'm happy to do it.

One existing difference isn't fixed here: reading a property through an instance (obj.prop, not super()) whose getter is a native callable with a __getattribute__ that blocks __class__ raises the error from that __getattribute__ in pyscript, on master too, while Python returns the value. That's because #918's property check uses isinstance() on the getter. It's a separate change, so I left it out of this PR.

super().prop and super(C, self).prop returned a coroutine instead of
the value, since the object is a super and pyscript only looked for a
property along type(obj).__mro__, so Python called the pyscript getter
itself. For a super object, pyscript now searches the mro of
__self_class__ after __thisclass__, as Python does, and awaits a
pyscript getter on a plain property with __self__. The super object's
members and each class's mro and dict are read through the built-in
descriptors, and the getter is checked by its real type, so a parent's
attribute with the same name, a metaclass's __getattribute__ or a
getter's own hooks can't change the lookup. As in Python, the getter's
AttributeError isn't passed to __getattr__, and a class's own
__getattribute__ isn't used. Everything else, including super(C, cls)
on a class, __class__, property subclasses and compiled getters, is
left to Python. Tests compare 28 scripts with CPython, and the docs no
longer list super().prop as not working.

Signed-off-by: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com>

@craigbarratt craigbarratt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @HoneyBearTech, looks good. Approving.

@craigbarratt
craigbarratt merged commit 2ae440d into custom-components:master Nov 22, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants